home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GP_ITBC.C < prev    next >
C/C++ Source or Header  |  1992-03-22  |  6KB  |  171 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gp_itbc.c */
  21. /* Intel processor, Turbo/Borland C-specific routines for Ghostscript */
  22. #include "dos_.h"
  23. #include <fcntl.h>
  24. #include <io.h>
  25. #include <signal.h>
  26. #include "string_.h"
  27. #include "gx.h"
  28. #include "gp.h"
  29. #ifdef __OVERLAY__
  30. #  include "overlay.h"
  31. #endif
  32.  
  33. /* Define the size of the C stack. */
  34. unsigned _stklen = 8000;        /* default is 4096, we need more */
  35.  
  36. /* Define the size of the overlay buffer, if relevant. */
  37. #ifdef __OVERLAY__
  38. unsigned _ovrbuffer = (1024L * OVLBUFK) / 16;
  39. #endif
  40.  
  41. /* Forward declarations */
  42. private void handle_FPE(P3(int, int, int *));
  43.  
  44. /* Do platform-dependent initialization */
  45. #if CPU_TYPE > 86
  46. /* Internal routine to set flags and read them back. */
  47. /* We use __emit__ so we don't require an assembler. */
  48. private int
  49. push_pop_flags(unsigned flags)
  50. {    __emit__(0x8b, 0x46, 6);    /* mov ax,flags */
  51.     __emit__(0x50, 0x9d);        /* push ax; popf */
  52.     __emit__(0x9c, 0x58);        /* pushf; pop ax */
  53. }
  54. #endif
  55. extern void gp_init_console(P0());
  56. void
  57. gp_init()
  58. {    /*
  59.      * Detect the processor type using the following algorithms:
  60.      *    The 8088/8086 truncate shift counts mod 32,
  61.      *      the 80186 and up do not.
  62.      *    The 80186 and below fix FLAGS bits 15-12 to 1,
  63.      *      the 80286 and up do not.
  64.      *    The 80386 allows setting FLAGS bits 14-12,
  65.      *      the 80286 and below do not.
  66.      * We currently can't tell an 80386 from an 80486.
  67.      * Note that this algorithm will identify an 80386
  68.      *   running in Virtual 8086 mode as an 80386.
  69.      *   This is acceptable, because Ghostscript doesn't actually
  70.      *   use 80286 or 80386 addressing modes, only the additional
  71.      *   instructions available on these processors.
  72.      * (This algorithm is derived from the Intel manuals.)
  73.      */
  74. #if CPU_TYPE > 86
  75.     /* We have to be careful not to turn interrupts off! */
  76.     int result, type;
  77.     result = push_pop_flags(0x202);
  78.     if ( (result & 0xf000) == 0xf000 )
  79.        {    /* CPU is an 8088/8086/80186 */
  80.            {    int shc = 33;    /* force shift by variable */
  81.             result = 0xffff << shc;
  82.            }
  83.         type = (result == 0 ? 186 : 86);
  84.        }
  85.     else
  86.        {    /* CPU is an 80286/80386/... */
  87.         result = push_pop_flags(0x7202);
  88.         type = ((result & 0x7000) == 0 ? 286 : 386);
  89.        }
  90.     /* A 486 is the same as a 386. */
  91. #define CPU_EQUIV (CPU_TYPE == 486 ? 386 : CPU_TYPE)
  92.     if ( type < CPU_EQUIV )
  93.        {    eprintf1("This executable requires an 80%d or higher.\n",
  94.              CPU_EQUIV);
  95.         exit(1);
  96.        }
  97. #endif
  98.     _fmode = O_BINARY;        /* Open files in 'binary' mode */
  99.  
  100. #ifdef __OVERLAY__
  101.     /* Initialize the overlay machinery. */
  102.        {    int code;
  103. #  ifdef OVEMS
  104.         code = _OvrInitEms(OVEMS_HANDLE, OVEMS_FIRST, OVEMS_PAGES);
  105.         if ( code )
  106.             eprintf("Attempt to use EMS memory for overlays failed.\n");
  107. #  endif
  108. #  ifdef OVEXT
  109.         code = _OvrInitExt(OVEXT_START, OVEXT_LENGTH);
  110.         if ( code )
  111.             eprintf("Attempt to use extended memory for overlays failed.\n");
  112. #  endif
  113.        }
  114. #endif
  115.     /* Set up the handler for numeric exceptions. */
  116.     signal(SIGFPE, handle_FPE);
  117.     gp_init_console();
  118. }
  119.  
  120. /* Trap numeric exceptions.  Someday we will do something */
  121. /* more appropriate with these. */
  122. private void
  123. handle_FPE(int sig, int subcode, int *regs)
  124. {    eprintf("Numeric exception:\n");
  125.     fprintf(estderr,
  126. "AX=%04x  BX=%04x  CX=%04x  DX=%04x  SI=%04x  DI=%04x  BP=%04x\n",
  127.         regs[8], regs[7], regs[6], regs[5], regs[2], regs[1], regs[0]);
  128.     fprintf(estderr,
  129. "DS=%04x  ES=%04x  CS:IP=%04x:%04x\n",
  130.         regs[3], regs[4], regs[10], regs[9]);
  131.     exit(1);
  132. }
  133.  
  134. /* ------ File names ------ */
  135.  
  136. /* Create and open a scratch file with a given name prefix. */
  137. /* Write the actual file name at fname. */
  138. FILE *
  139. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  140. {    strcpy(fname, prefix);
  141.     strcat(fname, "XXXXXX");
  142.     mktemp(fname);
  143.     return fopen(fname, mode);
  144. }
  145.  
  146. /* ------ File operations ------ */
  147.  
  148. /* If the file given by fname exists, fill in its status and return 1; */
  149. /* otherwise return 0. */
  150. int
  151. gp_file_status(const char *fname, file_status *pstatus)
  152. {    FILE *f = fopen(fname, "r");
  153.     long flen;
  154.     struct ftime ft;
  155.     if ( f == NULL ) return 0;
  156.     if ( getftime(fileno(f), &ft) < 0 )
  157.        {    fclose(f);
  158.         return 0;
  159.        }
  160.     fseek(f, 0, SEEK_END);
  161.     flen = ftell(f);
  162.     pstatus->size_pages = (flen + 1023) >> 10;
  163.     pstatus->size_bytes = flen;
  164.     /* Make a single long value from the ftime structure. */
  165.     pstatus->time_referenced = pstatus->time_created =
  166.       ((long)((ft.ft_year << 9) + (ft.ft_month << 5) + ft.ft_day) << 16) +
  167.       ((ft.ft_hour << 11) + (ft.ft_min << 5) + ft.ft_tsec);
  168.     fclose(f);
  169.     return 1;
  170. }
  171.